package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import ewms.bo.EwmsOutWarehouseAddBo;
import ewms.bo.EwmsOutWarehouseDetailAddBo;
import ewms.bo.EwmsOutWarehouseEditBo;
import ewms.bo.EwmsOutWarehouseQueryBo;
import ewms.domain.EwmsOutFactory;
import ewms.domain.EwmsOutWarehouse;
import ewms.domain.EwmsOutWarehouseDetail;
import ewms.domain.EwmsWasteInventory;
import ewms.enums.WastHandlerMethod;
import ewms.mapper.EwmsOutFactoryMapper;
import ewms.mapper.EwmsOutWarehouseDetailMapper;
import ewms.mapper.EwmsOutWarehouseMapper;
import ewms.mapper.EwmsWasteInventoryMapper;
import ewms.service.IEwmsOutWarehouseService;
import ewms.utils.EwmsUtils;
import ewms.vo.EwmsOutWarehouseVo;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.common.exception.CustomException;
import ewms.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 危废出库Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-16
 */
@Service
public class EwmsOutWarehouseServiceImpl extends ServiceImpl<EwmsOutWarehouseMapper, EwmsOutWarehouse> implements IEwmsOutWarehouseService {

	@Autowired
	private EwmsOutWarehouseDetailMapper outWarehouseDetailMapper;
	@Autowired
	private EwmsWasteInventoryMapper wasteInventoryMapper;
	@Autowired
	private EwmsOutFactoryMapper outFactoryMapper;

	@Override
	public EwmsOutWarehouseVo queryById(Long outWarehouseId) {
		return getVoById(outWarehouseId, EwmsOutWarehouseVo.class);
	}

	@Override
	public TableDataInfo<EwmsOutWarehouseVo> selectPagePreOutWarehouseList(EwmsOutWarehouseQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPagePreOutWarehouseList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsOutWarehouseVo> selectPreOutWarehouseList(EwmsOutWarehouseQueryBo bo) {
		return baseMapper.selectPreOutWarehouseList(bo);
	}

	@Override
	public TableDataInfo<EwmsOutWarehouseVo> selectPageOutWarehouseList(EwmsOutWarehouseQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPageOutWarehouseList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsOutWarehouseVo> selectOutWarehouseList(EwmsOutWarehouseQueryBo bo) {
		return baseMapper.selectOutWarehouseList(bo);
	}


	@Override
	public TableDataInfo<EwmsOutWarehouseVo> queryPageList(EwmsOutWarehouseQueryBo bo) {
		PagePlus<EwmsOutWarehouse, EwmsOutWarehouseVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsOutWarehouseVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsOutWarehouseVo> queryList(EwmsOutWarehouseQueryBo bo) {
		return listVo(buildQueryWrapper(bo), EwmsOutWarehouseVo.class);
	}

	private LambdaQueryWrapper<EwmsOutWarehouse> buildQueryWrapper(EwmsOutWarehouseQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsOutWarehouse> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getOutCode() != null, EwmsOutWarehouse::getOutCode, bo.getOutCode());
		lqw.eq(bo.getOutDate() != null, EwmsOutWarehouse::getOutDate, bo.getOutDate());
		return lqw;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean insertByAddBo(EwmsOutWarehouseAddBo bo) {
		// 保存出库主表信息
		boolean saveFlag = false;
		EwmsOutWarehouse add = BeanUtil.toBean(bo, EwmsOutWarehouse.class);
		add.setOutDate(EwmsUtils.handleSelectDate(add.getOutDate()));
		validEntityBeforeSave(add);
		String outCode = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT);
		add.setOutCode(outCode);
		saveFlag = save(add);
		// 保存出库明细信息
		List<EwmsOutWarehouseDetailAddBo> outWarehouseDetailList = bo.getOutWarehouseDetailList();
		for (EwmsOutWarehouseDetailAddBo outWarehouseDetail : outWarehouseDetailList) {
			// 校验处置公司是否为空
			if (WastHandlerMethod.out_factory.name().equals(outWarehouseDetail.getHandleMethod()) && StrUtil.isBlank(outWarehouseDetail.getHandleCompany())) {
				throw new CustomException("处置公司不能为空！");
			}
			EwmsWasteInventory ewmsWasteInventory = wasteInventoryMapper.selectById(outWarehouseDetail.getWasteInventoryId());
			if (Objects.isNull(ewmsWasteInventory)) {
				throw new CustomException("库存信息未找到");
			}

			EwmsOutWarehouseDetail addDetail = new EwmsOutWarehouseDetail();
			addDetail.setCreateBy(add.getCreateBy())
				.setCreateTime(add.getCreateTime())
				.setOutWarehouseId(add.getOutWarehouseId())
				.setWasteInventoryId(ewmsWasteInventory.getWasteInventoryId())
				.setHandleMethod(outWarehouseDetail.getHandleMethod())
				.setOutWeight(ewmsWasteInventory.getWasteWeight());
			saveFlag = saveFlag && SqlHelper.retBool(outWarehouseDetailMapper.insert(addDetail));

			//生成处置信息(如果是委外出厂)
			if (WastHandlerMethod.out_factory.name().equals(outWarehouseDetail.getHandleMethod())) {
				EwmsOutFactory ewmsOutFactory = new EwmsOutFactory();
				ewmsOutFactory.setWasteId(ewmsWasteInventory.getWasteId())
					.setWasteName(ewmsWasteInventory.getWasteName())
					.setHandleCompanyKey(outWarehouseDetail.getHandleCompany())
					.setOutWarehouseWeight(ewmsWasteInventory.getWasteWeight())
					.setOutWarehouseDetailId(addDetail.getOutWarehouseDetailId())
					.setOutFactoryDate(add.getOutDate())
					.setCreateTime(add.getCreateTime())
					.setCreateBy(add.getCreateBy());
				saveFlag = saveFlag && SqlHelper.retBool(outFactoryMapper.insert(ewmsOutFactory));
			}
		}


		return saveFlag;
	}

	@Override
	public Boolean updateByEditBo(EwmsOutWarehouseEditBo bo) {
		EwmsOutWarehouse update = BeanUtil.toBean(bo, EwmsOutWarehouse.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsOutWarehouse entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
